﻿#include <iostream>
#include <string>
#include <zmq.hpp>
#include <zmq_addon.hpp>

int main() {
  // initialize the zmq context with a single IO thread
  zmq::context_t context{1};

  zmq::socket_t socket{context, zmq::socket_type::sub};
  socket.connect("tcp://127.0.0.1:9528");

  // 设置订阅的topic
  socket.set(zmq::sockopt::subscribe, "myTopicA");
  socket.set(zmq::sockopt::subscribe,"myTopicC");

  socket.set(zmq::sockopt::subscribe,"##MyMessage");

  // 为空时订阅所有
  // socket.set(zmq::sockopt::subscribe,"");

  while (true) {
    std::vector<zmq::message_t> msgs;
    auto result = zmq::recv_multipart(socket, std::back_inserter(msgs));
    assert(result && "recv failed");
    assert(*result==2);

    std::cout<<msgs.at(0).to_string()<<" "<<msgs.at(1).to_string()<<std::endl;
  }
}
